// BcmLibTest.c
//
// A basic test of SPI transfers at ~1MHz SCLK with manually managed CS pins.
// The goal is to have a function that can be called by another program which
// will send a desired number of bytes to a given device.
//
// Based on the example code provided by Mike McCauley at
// http://www.open.com.au/mikem/bcm2835
//
// Author: Daniel Hearn
// Date: 23 Feb 2013

#include <bcm2835.h>
#include <stdio.h>
#include "BcmLib.h"

//****************************************************************************/
//	Global Variables
//****************************************************************************/
uint8_t v1CsPins[] = {RPI_GPIO_P1_07, RPI_GPIO_P1_11, RPI_GPIO_P1_12, // 4,17,18
                      RPI_GPIO_P1_13, RPI_GPIO_P1_15, RPI_GPIO_P1_16, // 21,22,23
                      RPI_GPIO_P1_18, RPI_GPIO_P1_22};                // 24,25

uint8_t v2CsPins[] = {RPI_V2_GPIO_P1_07, RPI_V2_GPIO_P1_11, RPI_V2_GPIO_P1_12,
                      RPI_V2_GPIO_P1_13, RPI_V2_GPIO_P1_15, RPI_V2_GPIO_P1_16,
                      RPI_V2_GPIO_P1_18, RPI_V2_GPIO_P1_22};

//****************************************************************************/
//	Main
//****************************************************************************/
int main(int argc, char **argv)
{
    if (initMySpi(v1CsPins, sizeof(v1CsPins)))
        return 1;

    char obuf[] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24};
    char ibuf[24];

    int i;
    for(i = 0; i < 100; i++){
        mySpiTransfer(obuf, ibuf, 24, v1CsPins[(i%8)]);
        //printf("Read from SPI: %02X  %02X  %02X  %02X \n", ibuf[0], ibuf[1], ibuf[2], ibuf[3]);
    }

    bcm2835_spi_end();
    bcm2835_close();

    return 0;
}














